﻿using CarparkEnum;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CarparkIMS_CMS.Models
{
    public partial class ResponseModalX
    {
        delegate T TransDataType<T>(T x);
        public ResponseModalX()
        {
            _meta = new MetaModalX { ErrorCode = -1, Success = true, Message = "OK" };  //(int)GeneralReturnCode.SUCCESS
            _data = null;
        }
        private MetaModalX _meta;
        [JsonProperty("meta")]
        public MetaModalX meta
        {
            get
            {
                return _meta;
            }
            set
            {
                _meta = value;
            }
        }
        private Object _data;
        [JsonProperty("data")]
        public Object data
        {
            get
            {
                return _data;
            }
            set
            {
                _data = value;
            }
        }

        public static implicit operator Task<object>(ResponseModalX v)
        {
            throw new NotImplementedException();
        }
    }

    public partial class MetaModalX
    {
        [JsonProperty("success")]
        public bool Success { get; set; } = true;

        [JsonProperty("message")]
        public string Message { get; set; } = "OK";

        [JsonProperty("errorCode")]
        public int ErrorCode { get; set; } = -1; //(int)GeneralReturnCode.SUCCESS
    }

    public class UserFile
    {
        public UserFile(IFormFile ifile)
        {
            File = ifile;
        }
        public string FileName { get; set; }

        public long Length { get; set; }

        public string Extension { get; set; }

        public string FileType { get; set; }

        private readonly static string[] Filters = { ".jpg", ".png", ".bmp" };

        public bool IsValid => !string.IsNullOrEmpty(this.Extension) && Filters.Contains(this.Extension);

        private IFormFile file;

        public IFormFile File
        {

            get
            {
                return file;
            }

            set
            {
                if (value != null)
                {
                    this.file = value;
                    this.FileName = this.file.FileName;
                    this.FileType = this.file.ContentType;

                    this.Length = this.file.Length;

                    this.Extension = this.file.FileName.Substring(file.FileName.LastIndexOf('.'));

                    if (string.IsNullOrEmpty(this.FileName))

                        this.FileName = this.FileName;
                }
            }
        }
        public async Task<ResponseModalX> SaveAs(string pathFilename)
        {
            ResponseModalX responseModalX = new ResponseModalX
            {
                meta = new MetaModalX { Success = true, ErrorCode = 0, Message = "FILE_UPLOAD_SUCCESS" }
            };

            if (this.file == null)
            {
                responseModalX = new ResponseModalX
                {
                    meta = new MetaModalX { Success = false, ErrorCode = (int)GeneralReturnCode.FAIL, Message = "FILE_UPLOAD_FAIL" }
                };
                return responseModalX;
            }

            using (FileStream fs = new FileStream(pathFilename, FileMode.CreateNew))
            {
                await this.file.CopyToAsync(fs);
                fs.Flush();
                fs.Close();
                responseModalX.data = pathFilename;
                return responseModalX;
            }
        }
    }
}
